/**
 * \brief  Read-only memory modules
 * \author Christian Helmuth
 * \date   2006-05-15
 */

/*
 * Copyright (C) 2006-2009 Christian Helmuth
 * Genode Labs, Feske & Helmuth Systementwicklung GbR
 *
 * This file is part of the Genode OS framework, which is distributed
 * under the terms of the GNU General Public License version 2.
 */

#ifndef _CORE__INCLUDE__ROM_FS_H_
#define _CORE__INCLUDE__ROM_FS_H_

#include <base/stdint.h>
#include <base/printf.h>
#include <util/avl_tree.h>
#include <util/avl_string.h>

namespace Genode {

	class Rom_module : public Avl_string_base
	{
		private:

			/* Location of module in memory and size */
			addr_t _addr;
			size_t _size;

		public:

			/** Standard constructor creates invalid object */
			Rom_module()
			: Avl_string_base(0), _addr(0), _size(0) { }

			Rom_module(addr_t addr, size_t size, const char *name)
			: Avl_string_base(name), _addr(addr), _size(size) { }

			/** Check validity */
			bool valid() { return _size ? true : false; }

			/** Accessor functions */
			addr_t      addr() const { return _addr; }
			size_t      size() const { return _size; }
	};

	class Rom_fs : public Avl_tree<Avl_string_base>
	{
		public:

			Rom_module * find(const char *name)
			{
				return first() ? (Rom_module *)first()->find_by_name(name) : 0;
			}

			/* DEBUG */
			void print_fs(Rom_module *r = 0)
			{
				if (!r) {
					printf("Rom_fs %p dump:\n", this);
					print_fs((Rom_module *)first());
				} else {
					Rom_module *child;

					printf(" Rom: [%08x,%08x) %s\n",
					       r->addr(), r->addr() + r->size(), r->name());

					if ((child = (Rom_module *)r->child(LEFT))) print_fs(child);
					if ((child = (Rom_module *)r->child(RIGHT))) print_fs(child);
				}
			}
	};
}

#endif /* _CORE__INCLUDE__ROM_FS_H_ */
